package com.java.business.system.database.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.system.database.bean.SyncBase;
import com.java.business.system.database.service.SyncBaseService;
import com.java.core.bean.ResultBean;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/system/database/syncbase")
@Controller
public class SyncBaseController {

	@Autowired
	private SyncBaseService service;

	@RequestMapping("/add")
	public void add(HttpServletResponse response, SyncBase base) {
		ResultBean rb = new ResultBean();
		try {
			service.add(base);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/update")
	public void update(HttpServletResponse response, SyncBase base) {
		ResultBean rb = new ResultBean();
		try {
			service.update(base);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/updateTotal")
	public void updateTotal(HttpServletResponse response) {
		ResultBean rb = new ResultBean();
		try {
			service.updateTotal();
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, SyncBase base) {
		ResultBean rb = new ResultBean();
		try {
			service.delete(base);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getList")
	public void getList(HttpServletResponse response, SyncBase base) {
		ResultBean rb = new ResultBean();
		try {
			List<SyncBase> list = service.getList(base);
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/sync")
	public void sync(HttpServletResponse response, String[] tables,
			String[] dblinks) {
		ResultBean rb = new ResultBean();
		try {
			service.sync(tables, dblinks);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}
}
